function gb2utf(gbStr) {
	var codeStr = getCodeStr();
	var codeRE = new RegExp("(%..%..)", "gi");
	var replaceRE = new RegExp("(%..%..)", "i");
	var gbCode;
	var utfCode;
	var gbStart;
	while ((codeRE.lastIndex < gbStr.length) && replaceRE.test(gbStr)) {
		codeRE.exec(gbStr);
		gbCode = new String(RegExp.$1);
		gbStart = new Number(codeStr.indexOf(gbCode.toLowerCase()));
		var utfStart = 0;
		if (gbStart != -1) {
			utfStart = gbStart + 7;
			utfCode = codeStr.substring(utfStart, utfStart + 6);
		} else {
			utfCode = "%u3000";
		}
		gbStr = gbStr.replace(replaceRE, utfCode);
	}
	return gbStr;
}

function getCodeStr() {
	return codestr;
}

function unicode(s) {
	var len = s.length;
	var rs = "";
	for ( var i = 0; i < len; i++) {
		var k = s.substring(i, i + 1);
		rs += "&#" + s.charCodeAt(i) + ";";
	}
	return rs;
}

function runicode(s) {
	var k = s.split(";");
	var r = "";
	for ( var x = 0; x < k.length; x++) {
		var m = k[x].replace(/&#/, "");
		r += String.fromCharCode(m);
	}
	return r;
}

function decodeURL(x) {
	return decodeURIComponent(x);
}


function build_msg( str_msg, mask ){
	str_msg = str_msg || "";
	mask = mask || false;
 
	var msg_len = Buffer.byteLength(str_msg,"utf-8"), packed_data;
	if( msg_len <= 0 ){
		return null;
	}
 
	if( msg_len < 126 ){
		packed_data = new Buffer(2+msg_len);
		packed_data[0] = 0x81;
		packed_data[1] = msg_len;
		packed_data.write( str_msg, 2 );
	}else if( msg_len <= 0xFFFF ){//用16位表示数据长度
		packed_data = new Buffer(4 + msg_len);
		packed_data[0] = 0x81;
		packed_data[1] = 126;
		packed_data.writeUInt16BE( msg_len, 2 );
		packed_data.write( str_msg, 4 );
	}else{//用64位表示数据长度
		/*packed_data = new Buffer(10+msg_len);
		packed_data[0] = 0x81;
		packed_data[1] = 127;
		packed_data.writeUInt32BE(msg_len & 0xFFFF0000 >> 32, 2);
		packed_data.writeUInt32BE(msg_len & 0xFFFF, 6);
		packed_data.write( str_msg, 10 );*/
	}
 
	return packed_data;
}

